package com.aidex.biz.service.impl;

import com.aidex.biz.service.LbsMapService;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.http.HttpUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信商户支付信息Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-21
 */
@Service
public class LbsMapServiceImpl implements LbsMapService {
    private static final Logger log = LoggerFactory.getLogger(LbsMapServiceImpl.class);
    /**
     * 腾讯地图 key
     */
    String txmapkey = "7NRBZ-6VI3O-CXBW5-S7KC2-DB4LF-Y4FFS";

    /**
     * 坐标转地址
     *
     * @param
     */
    public Map<String, String> getCityInfoByLatLng(String lat, String lng) {
        // https://lbs.qq.com/service/webService/webServiceGuide/webServiceGcoder
        Map<String, String> resultmap = new HashMap<>();
        String url = "https://apis.map.qq.com/ws/coord/v1/translate?locations=" + lat + "," + lng + "&type=1&key=" + txmapkey;
        String result = HttpUtils.doGet(url);
        JSONObject gcj20result = JSONObject.parseObject(result);
        if (gcj20result.getInteger("status") == 0) {
            JSONArray jsonArray = gcj20result.getJSONArray("locations");
            lat = (jsonArray.getJSONObject(0).getString("lat"));
            lng = (jsonArray.getJSONObject(0).getString("lng"));
        }
        url = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + lat + "," + lng + "&key=" + txmapkey+
                "&poi_options=address_format=short;policy=2";
        result = HttpUtils.doGet(url);
        String province = "", city = "", street = "", streetNumber = "", area = "", crossroad = "";
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject.getInteger("status") == 0) {
            try {
//                JSONObject jsonObjectarea = jsonObject.getJSONObject("result").getJSONObject("address_component");
//                area = StringUtils.isEmpty(jsonObjectarea.getString("district")) ? jsonObjectarea.getString("city") :
//                        jsonObjectarea.getString("district");
//                resultmap.put("area", area);
//                city = jsonObjectarea.getString("city");
//                resultmap.put("city", city);
//                province = jsonObjectarea.getString("province");
//                resultmap.put("province", province);
//                street = jsonObjectarea.getString("street");
//                resultmap.put("street", street);
//                streetNumber = jsonObjectarea.getString("street_number");
//                resultmap.put("streetNumber", streetNumber);
//                JSONObject address_reference = jsonObject.getJSONObject("result").getJSONObject("address_reference");
//                if (null == address_reference.getJSONObject("famous_area")) {
//                    crossroad = address_reference.getJSONObject("crossroad").getString("title");
//                } else {
//                    crossroad = address_reference.getJSONObject("famous_area").getString("title");
//                }
//
//                resultmap.put("crossroad", crossroad);
                JSONObject jsonObjectarea = jsonObject.getJSONObject("result").getJSONObject("address_reference");
                JSONObject landmark_l1=jsonObjectarea.getJSONObject("landmark_l1");
                JSONObject landmark_l2=jsonObjectarea.getJSONObject("landmark_l2");
                JSONObject street_number=jsonObjectarea.getJSONObject("street_number");
                String title=null;
                if(StringUtils.isNotNull(landmark_l2)){
                    title=landmark_l2.getString("title");
                }else{
                    if(StringUtils.isNotNull(landmark_l1)){
                        title=landmark_l1.getString("title");
                    }else{
                        title=street_number.getString("title");
                    }
                }
                resultmap.put("streetNumber", title);
            } catch (Exception e) {
                log.error(e.getMessage());
            }

        }

        return resultmap;
    }

    @Override
    public Map<String, String> translateLatLng(String lat, String lng) {
        Map<String, String> resultmap = new HashMap<>();
        String url = "https://apis.map.qq.com/ws/coord/v1/translate?locations=" + lat + "," + lng + "&type=1&key=" + txmapkey;
        String result = HttpUtils.doGet(url);
        JSONObject gcj20result = JSONObject.parseObject(result);
        if (gcj20result.getInteger("status") == 0) {
            JSONArray jsonArray = gcj20result.getJSONArray("locations");
            lat = (jsonArray.getJSONObject(0).getString("lat"));
            lng = (jsonArray.getJSONObject(0).getString("lng"));
            resultmap.put("lat",lat);
            resultmap.put("lng",lng);
        }
        return resultmap;
    }
}
